package com.lwj.liwanjia.view.activity.personal;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.EditPwdBean;
import com.lwj.liwanjia.bean.PwdBean;
import com.lwj.liwanjia.impl.EditPwdView;
import com.lwj.liwanjia.impl.EditZfPwdView;
import com.lwj.liwanjia.presenter.EditPwdPresnter;
import com.lwj.liwanjia.presenter.EditZfPwdPresenter;
import com.lwj.liwanjia.utils.SpfUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditPasswordActivity extends BaseActivity implements EditPwdView, EditZfPwdView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ed_yuanmima)
    EditText edYuanmima;
    @BindView(R.id.ed_newmima)
    EditText edNewmima;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv_forget)
    TextView tvForget;

    private String title;
    private EditPwdPresnter editPwdPresnter;
    private String token;
    private Map<String, Object> map = new HashMap<>();
    private EditZfPwdPresenter editZfPwdPresenter;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        title = getIntent().getStringExtra("title");
        if (title.equals("换绑手机")) {
            tv1.setText("原手机号");
            edYuanmima.setHint("请输入原手机号");
            edNewmima.setHint("请输入新手机号");
            tv2.setText("新手机号");
        } else if (title.equals("修改支付密码")) {
            tv1.setText("原支付密码");
            edYuanmima.setHint("请输入原6位数字密码");
            edYuanmima.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
            edYuanmima.setTransformationMethod(PasswordTransformationMethod.getInstance());
            edYuanmima.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)}); //最大输入长度
            edNewmima.setHint("请输入新6位数字密码");
            edNewmima.setInputType(EditorInfo.TYPE_CLASS_NUMBER);//输入类型
            edNewmima.setTransformationMethod(PasswordTransformationMethod.getInstance());//密码不可见
            edNewmima.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)}); //最大输入长度
            tv2.setText("新支付密码");
            tvForget.setVisibility(View.VISIBLE);
        }
        tvTitle.setText(title);
        token = SpfUtils.getSpfUtils(this).getToken();
        editPwdPresnter = new EditPwdPresnter(this);
        editZfPwdPresenter = new EditZfPwdPresenter(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_password;
    }

    @OnClick({R.id.tv_left, R.id.tv_sure, R.id.tv_forget})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_sure:
                String oldPwd = edYuanmima.getText().toString().trim();
                String newPwd = edNewmima.getText().toString().trim();
                if (title.equals("修改登录密码")) {
                    if (!oldPwd.matches("^[A-Za-z0-9]{6,16}+$") || !newPwd.matches("^[A-Za-z0-9]{6,16}+$")) {
                        Toast.makeText(this, "密码只能是数字、字母,以及长度是6-16位", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    map.put("token", token);
                    map.put("pwd", oldPwd);
                    map.put("newPwd", newPwd);
                    editPwdPresnter.editPwd(map);
                } else if (title.equals("修改支付密码")) {
                    if (!oldPwd.matches("^\\d{6}$") && !newPwd.matches("^\\d{6}$")) {
                        Toast.makeText(this, "密码只能是6位数字", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (oldPwd.equals(newPwd)) {
                        Toast.makeText(this, "新密码与旧密码相同", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    editZfPwdPresenter.editZfPwd(token, oldPwd, newPwd);
                } else {//换绑手机
                    startActivity(new Intent(this, EditCompleteActivity.class).putExtra("title", "换绑手机"));
                    finish();
                }
                break;
            case R.id.tv_forget://忘记密码
                startActivity(new Intent(this, SjYzActivity.class));
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "访问网络失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void editZfPwd(PwdBean pwdBean) {
        if (pwdBean.getState() == 0) {
            Toast.makeText(this, "修改支付密码失败", Toast.LENGTH_SHORT).show();
        } else if (pwdBean.getState() == 1) {
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, EditCompleteActivity.class).putExtra("title", title));
            finish();
        } else if (pwdBean.getState() == 3) {
            Toast.makeText(this, "原密码错误", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void editPwd(EditPwdBean editPwdBean) {
        if (editPwdBean.getState() == 0) {
            Toast.makeText(this, "修改登录密码失败", Toast.LENGTH_SHORT).show();
        } else if (editPwdBean.getState() == 1) {
            if (editPwdBean.getData() == 1) {
                Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, EditCompleteActivity.class).putExtra("title",title));
                finish();
            } else if (editPwdBean.getData() == 2) {
                Toast.makeText(this, "原密码错误", Toast.LENGTH_SHORT).show();
            } else if (editPwdBean.getData() == 3) {
                Toast.makeText(this, "新密码与旧密码相同", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (editPwdPresnter != null) {
            editPwdPresnter = null;
        }
        if (editZfPwdPresenter != null) {
            editZfPwdPresenter = null;
        }
    }
}
